package com.lianrui.app.goods.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianrui.app.comments.service.ICommentsService;
import com.lianrui.app.comments.vos.CommentsPage;
import com.lianrui.app.goods.domain.Goods;
import com.lianrui.app.goods.service.IGoodsService;
import com.lianrui.app.goods.service.IProductSerivce;
import com.lianrui.app.goods.vos.GoodsPage;
import com.lianrui.app.goods.vos.SpecVo;
import com.lianrui.app.order.services.IOrderItemService;
import com.lianrui.app.order.vos.OrderItemPage;
import com.lianrui.commons.ResultView;
import com.lianrui.commons.exception.BusinessException;
import com.lianrui.core.AbstractController;
import com.lianrui.core.IService;

/**
 * 功能描述：商品控制器
 *
 * @author LJQ
 *
 * 2016年1月5日 上午10:26:53
 * @param <D>
 */
@Controller
@RequestMapping("/goods")
public class GoodsController extends AbstractController<GoodsPage, Goods, Integer> {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(GoodsController.class);
	
	@Autowired
	IGoodsService goodsService;
	
	@Autowired
	ICommentsService commentsService;
	
	@Autowired
	IOrderItemService orderItemService;
	
	@Autowired
	IProductSerivce productSerivce;
	
	@Override
	public IService<GoodsPage, Goods, Integer> getService() {
		return goodsService;
	}

	@Override
	public Logger getLogger() {
		return LOGGER;
	}
	
	@RequestMapping("/getCount/{orderId}")
	@ResponseBody
	public ResultView<Map<String,Integer>> getStatisticsCount(@PathVariable Integer orderId) {
		ResultView<Map<String,Integer>> view = new ResultView<>();
		
		try {
			 int orderItem = goodsService.getOrderItemCount(orderId);
			 int comments  = commentsService.getCommentsCount(orderId);
			 
			 Map<String,Integer> maps = new HashMap<String, Integer>();
			 maps.put("orderTotal", orderItem);
			 maps.put("commentsTotal", comments);
			 view.setResult(maps);
		} catch (BusinessException e) {
			view.setCode(HttpStatus.EXPECTATION_FAILED.value());
			view.setMessage(e.getMessage());
		}
		
		
		return view;
	}
	
	@RequestMapping("/recommend/{num}")
	@ResponseBody
	public ResultView<List<Goods>> delete(@PathVariable int num) {
		ResultView<List<Goods>> view = new ResultView<List<Goods>>();
		try {
			List<Goods> recommends=goodsService.recommendGoods(num);
			view.setResult(recommends);
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.setMessage(e.getMessage());
		}
		return view;
	}
	
	@RequestMapping("/tradeRecord/{goods_id}")
	@ResponseBody
	public OrderItemPage listItem(OrderItemPage page, @PathVariable Integer goods_id) {
		page.setGoods_id(goods_id);
		try {
			orderItemService.list(page);
		} catch (BusinessException e) {
			page.setCode(e.getCode());
			page.setMessage(e.getMessage());
		}
		return page;
	}
	

	@RequestMapping("/commentsRecord/{goods_id}")
	@ResponseBody
	public CommentsPage listComments(CommentsPage page, @PathVariable Integer goods_id) {
		page.setGoods_id(goods_id);
		try {
			commentsService.list(page);
			page.setAppend(true);
			page.setAppendTotle(commentsService.count(page));
		} catch (BusinessException e) {
			page.setCode(e.getCode());
			page.setMessage(e.getMessage());
		}
		return page;
	}
	
	@RequestMapping("/getSpecs/{goodsId}")
    @ResponseBody
    public ResultView<List<SpecVo>> getSpecs(@PathVariable Integer goodsId) {
        ResultView<List<SpecVo>> view = new ResultView<List<SpecVo>>();
        try {
            List<SpecVo> spects = this.productSerivce.getSpecs(goodsId);
            view.setResult(spects);
        } catch (BusinessException e) {
            view.setCode(HttpStatus.EXPECTATION_FAILED.value());
            view.setMessage(e.getMessage());
        }

        return view;
    }
}
